*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	This is the Master program for building the analysis file as well as generating
*	MSE simulation first-step estimates for NYC high schools.
*	----------------------------------------------------------------------------

	clear all
	set more off
	set maxvar 	120000
	set emptycells drop

*	city

	global city 		NYC

*	paths

	global rawdata 				"${data_raw_NYC}"
	global cleandata 			"${data_clean_NYC}"
	global builddata 			"${data_build_NYC}"

*	sample

	if "$city" == "NYC"{
		local fg	 		9		//first grade
		local lg	 		9		//last grade
		local fy  			2012	//first year
		local ly	   		2014	//last year

		global sectors screened

		local min_enroll	1
		local min_at_risk	1
	}

	local sample "_`fg'_`lg'_`fy'_`ly'"
	local sch_res "_e`min_enroll'_q`min_at_risk'"

*	controls

	global bl_demos bl_female bl_black bl_white bl_hispanic bl_asian bl_fr_lunch bl_sped bl_ell
	global bl_scores bl_math bl_ela bl_math2 bl_ela2 bl_math3 bl_ela3

	global iv_controls "i.year##(i.grade c.($bl_demos) c.($bl_scores))"

*	settings

	local ptype form2
	local vtype hom
	local bw	"_ccftcovs_bwall"

*	switches

	//generate analysis files by estimating OLS VAMs and getting school characteristics
	local build 		1		//build analysis file
	local olsvam		1		//estimate OLS-VAM
	local schools		1		//generate school characteristics

	//generate file that has conventional VAM with older lagged scores
	local build_older	1		//build analysis file
	local olsvam_older	1		//estimate OLS-VAM

	//for MSE simulations
	local vams			1		//estimate OLS-VAMs and export for MSE calcs
	local cov	 		1		//stack alphas
	local stack	 		1		//stack alphas and reduced form
	local diagdiag		1		//get first-step estimates
	local zz			1		//create Z'Z matrix

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*	do

	if `build' 	 		do "${code_build}b_build" 				`fg' `lg' `fy' `ly' `min_enroll' `min_at_risk' `bw'

	if `olsvam'			do "${code_build}b_olsvam"				`sample' `sch_res' `vtype' `bw'

	if `schools'		do "${code_build}b_schools_${city}"		`bw'

	if `build_older' 	do "${code_build}b_build_older" 		`fg' `lg' `fy' `ly' `min_enroll' `min_at_risk' `bw'

	if `olsvam_older'	do "${code_build}b_olsvam_older"		`sample' `sch_res' `vtype' `ptype' `bw'

	if `vams'			do "${code_build}b_vams"				`sample' `sch_res' `ptype' `bw'

	if `cov' 	 		do "${code_build}b_cov"					`sample' `sch_res' `ptype' `bw'

	if `stack' 	 		do "${code_build}b_stack"				`sample' `sch_res' `ptype' `bw'

	if `diagdiag' 		do "${code_build}b_diagdiag"			`sample' `sch_res' `ptype' `bw'

	if `zz'				do "${code_build}b_zz"					`sample' `sch_res' `ptype' `vtype' `bw'
